<?php

abstract class BasePresenter extends /*Nette\Application\*/Presenter{
	
	public $system;
	public $session;
	private $address = array();
	public $messages = array();
	public $id_menu = false;
	
	protected function beforeRender(){

		
	}
	
	protected function startup(){
		
		parent::startup();
		
		$this->session = Environment::getSession("web");
		
		$this->system = SystemModel::getInstance();
		
		$this->template->add("menu", $this->system->getMenuWeb());
		// inicializace prekladu
		$this->system->translation = Translation::getInstance()->getStaticTranslation();
		
		$uri = $_SERVER["REQUEST_URI"];
		$uri = explode("/", $uri);
		
		$a = Environment::getApplication()->getRequests();
		$item = $this->system->getMenuItemById($a[0]->params[id_menu]);
		
		// TODO toto by chtelo prepsat, aby se nejdriv zjistilo nejvyssi polozka menu a pote se ulozi cele podmenu
		$exist = $this->system->getMenuItemByName($uri[count($uri)-3]);
		if(!empty($exist)){
			$item[0]["id_menu"] = $exist[0]["id_menu"];
			$this->system->getMenuChildrens($item[0], $iter, $it);
		}
		
		$this->id_menu = $a[0]->params[id_menu];
		$this->addSystemToNavigation();
		$this->template->navigation = $this->getNavigation();
		
		$it = false;
		$iter = "";
		$this->system->getMenuChildrens($item[0], $iter, $it);
		
		$this->template->request = Environment::getHttpRequest();
		$this->template->languages = $this->system->getLanguages();
		$this->template->translation = $this->system->translation;
		$this->template->actual_language = $this->session->id_language;
		$this->template->language = $this->system->getCurrentLanguage();
		$this->template->add("actual", $item);
		$this->template->defaultTitle = Environment::getConfig('variable')->defaultTitle;
		$this->template->separator = Environment::getConfig('variable')->separator;
		$this->template->baseUrl = Environment::getConfig('variable')->baseUrl;
		$this->template->config = $this->system->getOptions();
		// seo
		$this->template->add("title", (empty($item[0]["seo_title"]) ? $item[0]["name"] : $item[0]["seo_title"]));
		if(!empty($item[0]["seo_description"])){
			$tmp = $this->template->config;
			$tmp["seo_description"] = $item[0]["seo_description"];
			$this->template->config = $tmp;
		}
		
		$boxes = array();
		// inicializace boxu
		if(is_numeric($item[0]["id_menu"])) $boxes = $this->system->getBoxInMenuByIdMenu($item[0]["id_menu"]);
		
		foreach($boxes as $b){
			if($b["enable"]){
				$module = $this->system->getModuleById($b["id_module"]);
				$menu_item = $this->system->getMenuItemByModule($b["id_module"]);
				
				$name_of_object = $module[0]["presenter"]."Presenter";
				$object = new $name_of_object();
				if(is_object($object)){
					if(method_exists($object, "boxHTML")) $this->template->add("box_" . $module[0]["presenter"], $object->boxHTML($menu_item[0]["name_seo"]));
					if(method_exists($object, "boxHTML2")) $this->template->add("box2_" . $module[0]["presenter"], $object->boxHTML2($menu_item[0]["name_seo"]));
					
					if(method_exists($object, "getMessages")){
						
						$messages = $object->getMessages();
						
						foreach($messages as $m){
							$this->flashMessage($m["message"], $m["error"]);
						}
					}
				}
			}
		}
		
	}
	
	public function addToNavigation($address){
		$address["address"] = substr($address["address"], 1);
		$this->address[] = $address;
		
		$this->template->navigation = $this->address;
	}
	
	public function addSystemToNavigation(){
		
		$uri = Environment::getHttpRequest()->uri->path;
		$uri = substr($uri, 1, -1);
		
		$address = explode("/", $uri);
		$baseUri = substr(Environment::getVariable("baseUri"), 1, -1);
		
		$lang = $this->system->getCurrentLanguage();
		$lang_abbr = ($lang[0]["default"] == 0 ? $lang[0]["abbr"] . "/" : "");
		
		$start = $this->system->getMainMenuItem();
		$this->address[] = array(
								"address" => (!empty($baseUri) ? $baseUri."/" : "") . $lang_abbr . $start["name_seo"]."/",
								"name" => Translation::getStaticTranslationByKey("home"),
								"start" => 1
								);
		
		$adresa = (!empty($baseUri) ? $baseUri."/" : "");
		
		foreach($address as $a){
			
			if($a != $baseUri && $a != $start["name_seo"]){
				$menu = $this->system->getMenuItemByname($a);
				if(!empty($menu)){
					$adresa .= $a."/";
					
					$this->address[] = array(
											"address" => $adresa,
											"name" => $menu[0]["name"]	
											);
				}
			}
		}

	}
	
	public function getNavigation(){

		return $this->address;
	}
	
	public function isValidEmail($email){
		return ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
	}
	
 
	function explodei($separator, $string, $limit = false ) { 
	   $len = strlen($separator); 
	   for ( $i = 0; ; $i++ ) 
	   { 
	       if ( ($pos = stripos( $string, $separator )) === false || ($limit !== false && $i > $limit - 2 ) ) 
	       { 
	           $result[$i] = $string; 
	           break; 
	       } 
	       $result[$i] = substr( $string, 0, $pos ); 
	       $string = substr( $string, $pos + $len ); 
	   } 
	   return $result; 
	}
	
	public function getMessages(){
		
		return $this->messages;
	}
}
?>